package com.witdata.common.domain.dto;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.witdata.common.domain.enums.ActiveStatus;
import com.witdata.common.domain.enums.classroom.Term;
import com.witdata.common.domain.enums.isAllow;
import com.witdata.common.domain.vo.CourseVo;
import com.witdata.common.serializer.ToLongDeSerializer;
import com.witdata.common.util.IdUtils;
import com.witdata.mysql.domain.entity.TCourses;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;


@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class CourseDto {

    @JsonSerialize(using = ToStringSerializer.class)
    @JsonDeserialize(using = ToLongDeSerializer.class)
    private Long id;
    
    private String name;

    private String code;

    private Term term;
    
    private String description;

    private String coverImage;

    private String requirements;

    private isAllow allowFreeJoin;

    private isAllow enableAssignment;

    private isAllow enableExam;

    private String joinCode;

    private Integer maxStudent;

    private ActiveStatus status;

    private Long creatorId;

    private LocalDateTime createdAt;

    private LocalDateTime updatedAt;

    public CourseDto(CourseVo courseVo) {
        this.id = IdUtils.createId();
        this.name = courseVo.getName();
        this.code = courseVo.getCode();
        this.term = Term.getTermByCode(courseVo.getTerm());
        this.description = courseVo.getDescription();
        this.coverImage = courseVo.getCoverImage();
        this.requirements = courseVo.getRequirements();
        this.allowFreeJoin = isAllow.ALLOW;
        this.enableAssignment = isAllow.ALLOW;
        this.enableExam = isAllow.ALLOW;
        this.joinCode = courseVo.getJoinCode();
        this.maxStudent = courseVo.getMaxStudent();
        this.status = ActiveStatus.ACTIVE;
        this.creatorId = Long.valueOf(courseVo.getTeacherId());
        this.createdAt = LocalDateTime.now();
        this.updatedAt = LocalDateTime.now();
    }

    public CourseDto(TCourses tCourses) {
        this.id = tCourses.getId();
        this.name = tCourses.getName();
        this.code = tCourses.getCode();
        this.term = tCourses.getTerm()?Term.UP:Term.DOWN;
        this.description = tCourses.getDescription();
        this.coverImage = tCourses.getCoverImage();
        this.requirements = tCourses.getRequirements();
        this.allowFreeJoin = tCourses.getAllowFreeJoin()?isAllow.ALLOW:isAllow.NOT_ALLOW;
        this.enableAssignment = tCourses.getEnableAssignment()?isAllow.ALLOW:isAllow.NOT_ALLOW;
        this.enableExam = tCourses.getEnableExam()?isAllow.ALLOW:isAllow.NOT_ALLOW;
        this.joinCode = tCourses.getJoinCode();
        this.maxStudent = tCourses.getMaxStudents();
        this.status = tCourses.getStatus()?ActiveStatus.ACTIVE:ActiveStatus.INACTIVE;
        this.creatorId = Long.valueOf(tCourses.getCreatorId());
        this.createdAt = tCourses.getCreatedAt().toLocalDateTime();
    }

    public TCourses toTCourses() {
        return TCourses.builder()
            .id(id)
            .name(name)
            .code(code)
            .term(term.getCode()==0)
            .description(description)
            .coverImage(coverImage)
            .requirements(requirements)
            .allowFreeJoin(allowFreeJoin.getCode()==0)
            .enableAssignment(enableAssignment.getCode()==0)
            .enableExam(enableExam.getCode()==0)
            .joinCode(joinCode)
            .maxStudents(maxStudent)
            .status(status.getCode()==0)
            .creatorId(creatorId.toString())
            .createdAt(Timestamp.valueOf(createdAt))
            .updatedAt(Timestamp.valueOf(updatedAt))
            .build();
    }   

}
